//
//  TGPreviewFileView.m
//  TongGuanLease
//
//  Created by 张奇 on 2022/6/28.
//

#import "TGPreviewFileView.h"
#import "IMYWebView.h"

@interface TGPreviewFileView()<IMYWebViewDelegate ,WKScriptMessageHandler>
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) IMYWebView *webView;

@property (nonatomic, copy) NSString *title;
@end

@implementation TGPreviewFileView

- (instancetype)initWithTitle:(NSString *)title height:(CGFloat)height {
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)]) {
        self.backgroundColor = UIColor.whiteColor;
        self.title = title;
        [self createUI];
    }
    return self;
}

- (instancetype)init {
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, RatioHeight(530))]) {
        self.backgroundColor = UIColor.whiteColor;
        [self createUI];
    }
    return self;
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
    if ([message.name isEqualToString:@"routeBack"]) {
        if (self.webView.canGoBack) {
            [self.webView goBack];
        }else {
            [self popBackAction];
        }
    }
    
}


-(void)popBackAction{
    [self dimss];
}


-(void)createUI{
    
    CGFloat navHeight = self.height==SCREEN_HEIGHT ? NavigationContentTopConstant: RatioWidth(40);
    
    UIView *navView = [[UIView alloc] init];
    navView.frame = CGRectMake(0, 0, self.width, navHeight);
    [self addSubview:navView];
    
    QMUIButton *backBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:kImage(@"black_back_icon") forState:UIControlStateNormal];
    [backBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        [self dimss];
    }];
    [navView addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(RatioWidth(10));
        make.bottom.equalTo(navView).offset(-RatioWidth(5));
        make.size.mas_equalTo(CGSizeMake(RatioWidth(30), RatioWidth(30)));
    }];
    
    if (!EmptyCheck(self.title)) {
        QMUILabel *titleLabel = [[QMUILabel alloc] init];
        titleLabel.text = self.title;
        titleLabel.textColor = DeepBlackColor;
        titleLabel.font = TGAutoFont(16);
        [navView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(navView);
            make.centerY.equalTo(backBtn);
        }];
    }
    
    _webView = [[IMYWebView alloc] initWithFrame:CGRectMake(0, navHeight, self.width, self.height-navHeight)];
    _webView.delegate = (id)self;
    [self addSubview:_webView];

}


- (void)webViewDidFinishLoad:(IMYWebView*)webView didFinishNavigation:(WKNavigation*)navigation{
//    _loadView.hidden = !_loadView.hidden;
    _webView.hidden = !_webView.hidden;
}

-(void)setLinkStr:(NSString *)linkStr{
    if (!EmptyCheck(linkStr)) {
        _webView.hidden = YES;
//        _loadView.hidden = NO;
        NSString *urlStringUTF8 = [linkStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL*url = [NSURL URLWithString:urlStringUTF8];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [_webView loadRequest:request];
        [_webView reload];
    }
}

- (void)setRequest:(NSURLRequest *)request {
    _request = request;
    if (request != nil) {
        _webView.hidden = YES;
        [_webView loadRequest:request];
        [_webView reload];
    }
}

- (void)show{
    [self tgShow];
}

- (void)dimss{
    [self tgHide];
}

@end
